package Public;

import java.nio.charset.Charset;
import java.util.HashMap;

public class ByteReader
{
	 byte[] mBuffer;
     int mOffset = 0;

     public ByteReader(byte[] bytes) { mBuffer = bytes; }

     /// <summary>
     /// Whether the buffer is readable.
     /// </summary>

     public boolean canRead;
     
    
     
     public boolean isCanRead()
	{
		return(mBuffer != null && mOffset < mBuffer.length);
	}



     private static final Charset utf8 = Charset.forName("utf-8");

     /// <summary>
     /// Read a single line from the buffer.
     /// </summary>

     static String ReadLine(byte[] buffer, int start, int count)
     {
    	
    	return  new String(buffer, start, count, utf8);
     }

     /// <summary>
     /// Read a single line from the buffer.
     /// </summary>

     public String ReadLine()
     {
         int max = mBuffer.length;

         // Skip empty characters
         while (mOffset < max && mBuffer[mOffset] < 32) ++mOffset;

         int end = mOffset;

         if (end < max)
         {
             for (; ; )
             {
                 if (end < max)
                 {
                     int ch = mBuffer[end++];
                     if (ch != '\n' && ch != '\r') continue;
                 }
                 else ++end;

                 String line = ReadLine(mBuffer, mOffset, end - mOffset - 1);
                 mOffset = end;
                 return line;
             }
         }
         mOffset = max;
         return null;
     }

     /// <summary>
     /// Assume that the entire file is a collection of key/value pairs.
     /// </summary>

     public HashMap<String, String> ReadDictionary()
     {
    	 HashMap<String, String> dict = new HashMap<String, String>();
//         char[] separator = new char[] { '=' };

         while (canRead)
         {
             String line = ReadLine();
             if (line == null) break;


             String[] split = line.split(" = ", 2);
             

             if (split.length == 2)
             {
                 String key = split[0].trim();
                 String val = split[1].trim();
                 dict.put(key, val);
             }
         }
         return dict;
     }
}
